package com.ruoyi.business.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.business.domain.Beds;
import com.ruoyi.business.domain.vo.BedsVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface BedsPlusMapper  extends BaseMapper<Beds> {
     // 只有床位为空的时候才能删除
    @Delete("DELETE FROM beds WHERE id = #{id} and employee_id IS NULL")
    void TrueDelete(@Param("id") Long id);

    /**
     * 根据dormitoryid获取所有床位，包含所在床位的人员信息
     * * * */
    @Select("SELECT \n" +
        "    beds.id,\n" +
        "    beds.dormitory_id,\n" +
        "beds.bedcode,\n" +
        "beds.`status`,\n" +
        "    beds.employee_id,\n" +
        "    beds.update_time,\n" +
        "    employee.full_name,\n" +
        "    employee.contact_number,\n" +
        "    employee.department,\n" +
        "    employee.job_number,\n" +
        "    accommodation_fee.fee_date,\n" +
        "    accommodation_fee.fee_amount,\n" +
        "    accommodation_fee.air_conditioning_fee,\n" +
        "    accommodation_fee.LEVEL,\n" +
        "    accommodation_fee.rent \n" +
        "FROM \n" +
        "    beds\n" +
        "LEFT JOIN \n" +
        "    employee ON beds.employee_id = employee.id\n" +
        "LEFT JOIN \n" +
        "    accommodation_fee ON accommodation_fee.room_id = beds.id \n" +
        "                    AND accommodation_fee.employee_id = beds.employee_id \n" +
        "                    AND accommodation_fee.create_time = (\n" +
        "                        SELECT MAX(create_time) \n" +
        "                        FROM accommodation_fee \n" +
        "                        WHERE room_id = beds.id \n" +
        "                        AND employee_id = beds.employee_id\n" +
        "                    )\n" +
        "WHERE \n" +
        "    beds.dormitory_id = #{dormitoryid};\n")
    List<BedsVo> getbedListBydormitoryid(@Param("dormitoryid") Long dormitoryid);

    @Select("   SELECT\n" +
        "        CONCAT(\n" +
        "            dormitory.building_name,\n" +
        "            '-',\n" +
        "            dormitory.floor,\n" +
        "            '-',\n" +
        "            dormitory.dormitory_id,\n" +
        "            '-',\n" +
        "            beds.bedcode\n" +
        "            ) AS beddetail FROM beds  left join dormitory on beds.dormitory_id=dormitory.id where beds.id=#{id}")
    String getbedDetailByid(@Param("id") String id);

    /**
     * 根据dormitoryid获取所有床位，包含所在床位的人员信息
     * * * */
    @Select("SELECT beds.* FROM beds WHERE beds.dormitory_id =(SELECT id FROM dormitory WHERE dormitory_id=#{dormitoryid} and building_name=#{buildingname}) and beds.`status`='空位' and beds.del_flag=0")
    List<Beds> getbedListBydormitory(@Param("dormitoryid") String dormitoryid,@Param("buildingname") String buildingname);
}
